package com.example.idea.controller;

import com.example.idea.entity.Orders;
import com.example.idea.service.OrdersService;
import com.example.idea.utils.Page;
import com.example.idea.utils.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/orders")
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    /**
     * 添加新闻
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Orders orders) {
        ordersService.add(orders);
        return Result.success(orders);
    }

    /**
     * 修改新闻
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Orders orders) {
        log.info("修改用户:{}", orders);
        ordersService.update(orders);
        return Result.success(orders);
    }

    /**
     * 删除新闻
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        ordersService.delete(id);
        return Result.success(null);
    }

    /**
     * 根据ID查询新闻
     */
    @RequestMapping("/query/{id}")
    public Result query(@RequestParam Integer id) {
        Orders orders = ordersService.query(id);
        return Result.success(orders);
    }

    /**
     * 批量删除新闻语句
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        ordersService.deleteBatch(ids);
        return Result.success(null);
    }

    /**
     * 查询全部新闻信息
     */
    @RequestMapping("/list")
    public Result list() {
        log.info("查询全部用户信息");
        List<Orders> orders = ordersService.list();
        return Result.success(orders);
    }

    /**
     * 根据不同条件进行查询数据
     */
    @RequestMapping("/selectList")
    public Result list(@RequestBody Orders orders) {
        List<Orders> order = ordersService.selectList(orders);
        return Result.success(order);
    }

    @RequestMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String category,
                               @RequestParam String name) {
        log.info("分页查询，页码：{},页大小：{},用户名：{},姓名：{}", pageNum, pageSize, category, name);
        Page<Orders> page = ordersService.selectByPage(pageNum, pageSize, category, name);
        return Result.success(page);
    }

}
